﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace wpf.Nursing.Views
{
    /// <summary>
    /// DrrawNursing.xaml 的交互逻辑
    /// </summary>
    public partial class DrawNursing : UserControl
    {
        /// <summary>
        /// 构造方法
        /// </summary>
        public DrawNursing()
        {
            InitializeComponent();
            this.Loaded += DrrawNursing_Loaded;
          
        }
        /// <summary>
        /// 加载完毕
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DrrawNursing_Loaded(object sender, RoutedEventArgs e)
        {
            Window window = Window.GetWindow(MainCanvas);
            Point point = MainCanvas.TransformToAncestor(window).Transform(new Point(0, 0));
            VitStartPoint = VitStartPanel.TranslatePoint(new Point(), MainCanvas);
            DrawVitDataTable();
        }
        #region 属性
        /// <summary>
        /// 生命体征绘制开始坐标
        /// </summary>
        Point VitStartPoint = new Point();
        /// <summary>
        /// 展示几天的数据
        /// </summary>
        int Days = 7;
        /// <summary>
        /// 一天占几个单元小格
        /// </summary>
        int OneDayCell = 6;
        /// <summary>
        /// 单元格宽度
        /// </summary>
        double CellWidth = 18;
        /// <summary>
        /// 单元格高度
        /// </summary>
        double CellHeight = 18;
        /// <summary>
        /// 列数
        /// </summary>
        int RowCount = 35;
        #endregion

        /// <summary>
        /// 打印画布
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnPrint_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                System.Windows.Controls.PrintDialog dialog = new System.Windows.Controls.PrintDialog();

                if (dialog.ShowDialog() != true)
                    return;
                dialog.PrintVisual(this.MainCanvas, "IFMS Print Screen");
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message, "Print Screen", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        /// <summary>
        /// 绘制生命体征网格
        /// </summary>
        public void DrawVitDataTable()
        {

            //MainGrid.Loaded
            int rowCount = 35;
            int colCount = 42;
            int startRow = 5;
            int startCol = 6;
            #region 表头

            for (int _Col = 0; _Col < colCount; _Col++)
            {
                int rel = _Col % 3;

                int ColIndex = _Col + startCol;
                if (rel == 0)
                {
                    StackPanel header = new StackPanel();
                    // header.Name = "Cell" + _Row + "_" + _Col;
                    Grid.SetRow(header, 3);
                    Grid.SetColumn(header, ColIndex);
                    Grid.SetColumnSpan(header, 3);
                    MainGrid.Children.Add(header);
                    TextBlock headtxt = new TextBlock();
                    headtxt.Text = "上午";
                    header.Children.Add(headtxt);
                }


                StackPanel border = new StackPanel();
                //border.BorderBrush = new SolidColorBrush(Colors.Black);
                //border.BorderThickness = new Thickness(1);
                border.Name = "Cell" + 4 + "_" + _Col;
                Grid.SetRow(border, 4);
                Grid.SetColumn(border, ColIndex);
                MainGrid.Children.Add(border);
                TextBlock tb = new TextBlock();
                switch (rel)
                {
                    case 0:
                        tb.Text = "2";
                        break;
                    case 1:
                        tb.Text = "6";
                        break;
                    case 2:
                        tb.Text = "10";
                        break;

                }
                border.Children.Add(tb);
            }

            #endregion

            #region 表格主体
            for (int _Row = 0; _Row < rowCount; _Row++)
            {
                for (int _Col = 0; _Col < colCount; _Col++)
                {

                    int RowIndex = _Row + startRow;
                    int ColIndex = _Col + startCol;
                    if (_Row == 0 && _Col == 0)
                    {
                        continue;
                    }
                    StackPanel border = new StackPanel();
                    border.Name = "Cell" + _Row + "_" + _Col;
                    Grid.SetRow(border, RowIndex);
                    Grid.SetColumn(border, ColIndex);
                    MainGrid.Children.Add(border);
                }
            }
            #endregion

            #region 表格红线
            for (int i = 1; i <= 6; i++)
            {
                double x = OneDayCell * i * CellWidth + VitStartPoint.X;
                double y = CellHeight * RowCount + VitStartPoint.Y;
                Point startPoint = new Point() { X = x, Y = VitStartPoint.Y };
                Point endPoint = new Point() { X = x, Y = y };
                DrawLine(MainCanvas, startPoint, endPoint);
            }

            #endregion
        }
        /// <summary>
        /// 加载数据
        /// </summary>
        public void LoadData()
        {
             Window window = Window.GetWindow(MainCanvas);
             Point point = MainCanvas.TransformToAncestor(window).Transform(new Point(0, 0));
            VitStartPoint = VitStartPanel.TranslatePoint(new Point(), MainCanvas);

            //Label lb = new Label();
            //lb.HorizontalAlignment = HorizontalAlignment.Center;
            //lb.VerticalAlignment = VerticalAlignment.Center;
            //lb.Width = 20;
            //lb.Height = 20;
            //Color color2 = (Color)ColorConverter.ConvertFromString("Blue");
            //lb.Background = new SolidColorBrush(color2);
            //Color color = (Color)ColorConverter.ConvertFromString("Red");
            //lb.Foreground = new SolidColorBrush(color);
            //lb.Content = "★";
            //this.MainCanvas.Children.Add(lb);
            //var left = Canvas.GetLeft(bor1);
            //var top = Canvas.GetTop(bor1);
            //Canvas.SetLeft(lb, point2.X);
            //Canvas.SetTop(lb, point2.Y);
            // Drawlabel(MainCanvas, point2, "★");
            // DrawLine(MainCanvas, new Point() { X = 0, Y = 0 }, point2);
            GetVitDataAndDraw(VitStartPoint);
        }
        /// <summary>
        /// 模拟体温数据 第几天，温度
        /// </summary>
        public void GetVitDataAndDraw(Point StartPoint)
        {
            Dictionary<int, Double> Dict = new Dictionary<int, double>();

            Dict.Add(1, 37);
            Dict.Add(2, 36);
            Dict.Add(3, 39);
            Dict.Add(4, 40);
            Dict.Add(5, 36.5);

            double max = 42;
            double ku = 18;
            Point pt = new Point();
            int index = 0;
            foreach (var item in Dict)
            {

                double Move_x = item.Key * 18;
                double Move_y = (42 - item.Value) * 18 * 5;
                Point SetPoint = new Point();
                SetPoint.X = StartPoint.X + Move_x;
                SetPoint.Y = StartPoint.Y + Move_y;
                Drawlabel(MainCanvas, SetPoint, "●");
                if (index != 0)
                {
                    DrawLine(MainCanvas, pt, SetPoint);
                }
                pt = SetPoint;
                index++;
            }

        }

        public void DrawLine(Canvas MainCanvas, Point a, Point b)
        {
            //画个线
            var line = new Line();
            line.Stroke = Brushes.Red;
            line.StrokeThickness = 1;
            line.X1 = a.X;
            line.Y1 = a.Y;
            line.X2 = b.X;
            line.Y2 = b.Y;
            MainCanvas.Children.Add(line);
        }
        /// <summary>
        /// 输出图形字体
        /// </summary>
        /// <param name="MainCanvas"></param>
        /// <param name="target"></param>
        public void Drawlabel(Canvas MainCanvas, Point target, string txt)
        {
            Color color2 = (Color)ColorConverter.ConvertFromString("Blue");
            Color color = (Color)ColorConverter.ConvertFromString("Red");
            int ImgFontWeight = 20;
            int ImgFontHeight = 20;
            //画个文字
            TextBlock lb = new TextBlock();
            lb.HorizontalAlignment = HorizontalAlignment.Center;
            lb.VerticalAlignment = VerticalAlignment.Center;
            // lb.SetValue()
            lb.TextAlignment = TextAlignment.Center;
            lb.Foreground = new SolidColorBrush(color);
            lb.Width = ImgFontWeight;
            lb.Height = ImgFontHeight;
            //lb.Background = new SolidColorBrush(color2);
            lb.Text = txt;
            this.MainCanvas.Children.Add(lb);

            Canvas.SetLeft(lb, target.X - (ImgFontWeight / 2));
            Canvas.SetTop(lb, target.Y - (ImgFontHeight / 2));
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            LoadData();
        }
    }

}
